package com.fernandomdo.ServiceAdministrator;

// Android Imports
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.fernandomdo.ServiceAdministrator.databaseInterface.DatabaseInterface;
import com.fernandomdo.ServiceAdministrator.databaseInterface.DatabaseInterface.DatabaseConstants;

public class EditDialog extends DialogFragment implements OnClickListener {
	private static final String TAG = "EditDialog";
	
	public final static String EDIT = "edit_dialog";
	public final static String TERRITORY_ID = "territory_id";
	private Long territoryId;
	
	private EditText name;
	private EditText description;
	private Button confirm;
	private Button cancel;
	private Context appContext;
	
	private static DatabaseInterface databaseInterface;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		appContext = getActivity().getApplicationContext();
		databaseInterface = new DatabaseInterface(appContext);
		territoryId = this.getArguments().getLong(EditDialog.TERRITORY_ID);
		Log.d(TAG, "territoryId=" + territoryId);
		
		//Pick a style based on the sum
		int style = DialogFragment.STYLE_NORMAL, theme = 0;
		setStyle(style, theme);
	    
	    //getLoaderManager().initLoader(0, null, this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if(databaseInterface.readableDB.isOpen()) {
			databaseInterface.readableDB.close();			
		}
	}
	
	@Override
	public void onStop() {
		super.onStop();
		if(databaseInterface.readableDB.isOpen()) {
			databaseInterface.readableDB.close();			
		}
	}

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dlg_edit, container, false);
		
		name 		= (EditText)view.findViewById(R.id.edit_name);
		description = (EditText)view.findViewById(R.id.edit_description);
		confirm		= (Button)view.findViewById(R.id.btn_edit_confirm);
		cancel		= (Button)view.findViewById(R.id.btn_edit_cancel);
		
		databaseInterface.readableDB = databaseInterface.databaseHelper.getReadableDatabase();


		final String[] FROM = { BaseColumns._ID, DatabaseConstants.TERRITORY };
		final String SELECTION = BaseColumns._ID+"="+territoryId;

		Cursor oldName = databaseInterface.readableDB.query(DatabaseConstants.TERRITORY_TABLE,  FROM, SELECTION, null, null, null, null);
		Cursor oldDescription = databaseInterface.readableDB.query(DatabaseConstants.TERRITORY_INFO_TABLE, 
				new String[]{ DatabaseConstants.TERRITORY_ID, DatabaseConstants.DESCRIPTION }, 
				DatabaseConstants.TERRITORY_ID+"="+territoryId, 
				null, null, null, null);
		oldName.moveToFirst();
		oldDescription.moveToFirst();
		
		name.setText(Util.cursorFetchString(oldName, DatabaseConstants.TERRITORY));
		description.setText(Util.cursorFetchString(oldDescription, DatabaseConstants.DESCRIPTION));
		
		confirm.setOnClickListener(this);
		cancel.setOnClickListener(this);
		
		return view;
    }

	
	@Override
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.btn_edit_confirm:
				ContentValues territoryValues = new ContentValues();
				ContentValues territoryInfoValues = new ContentValues();
				
				territoryValues.put(DatabaseConstants.TERRITORY, String.valueOf(this.name.getText()));
				territoryInfoValues.put(DatabaseConstants.DESCRIPTION, String.valueOf(this.description.getText()));
				
				databaseInterface.readableDB.update(DatabaseConstants.TERRITORY_TABLE, territoryValues, 
						BaseColumns._ID+"=?", new String[] { String.valueOf(territoryId) });
				
				databaseInterface.readableDB.update(DatabaseConstants.TERRITORY_INFO_TABLE, territoryInfoValues, 
						DatabaseConstants.TERRITORY_ID+"=?", new String[] { String.valueOf(territoryId) });
				
				dismiss();
				ListTerritoryFragment.refreshList();
				break;
			case R.id.btn_edit_cancel:
				dismiss();
				break;
	    	}
	    }
}
